package net.bitdixit.ywu.library;

import net.bitdixit.ywu.R;
import net.bitdixit.ywu.preferences.AppPreferences;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class AsanaListActivity extends ListActivity {
	
	
	class AsanaArrayAdapter extends ArrayAdapter<Asana>
	{
	    private LayoutInflater mInflater;
	    AsanaLibraryView libraryView;
		public AsanaArrayAdapter(Context context, AsanaLibraryView libraryView) {
			super(context, R.layout.asana_list_item, libraryView.asanas());
			mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.libraryView = libraryView;
		}
		@Override 
		public View getView(int position, View convertView, ViewGroup parent)
		{
	        View view;
	       
	        if (convertView == null) {
	            view = mInflater.inflate(R.layout.asana_list_item, parent, false);
	        } else {
	            view = convertView;
	        }

	        TextView text = (TextView) view;
	        text.setText(getItem(position).getName(libraryView.viewData().getLang()));

	        return view;
		}
		
	}
	
	AsanaLibrary     library;
	AsanaLibraryView libraryView;
    private AppPreferences  appPreferences;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);

	     library = new AsanaLibrary(this);
	     appPreferences = new AppPreferences(getApplicationContext());
	     libraryView = new AsanaLibraryView(library, appPreferences);
	     libraryView.viewData().unserialize(appPreferences.getAsanaLibraryViewPreferences());
	     libraryView.apply();
	   	 resetView();
	   	 
	}
	
	@Override
	protected void onResume()
	{
	     super.onResume();
	     if (libraryView.viewData().isOnlyStared())
	     {
		     appPreferences = new AppPreferences(getApplicationContext());
		     resetView();
	     }
	};
	public void showAsanaInfo(Asana asana) 
	{
	    Intent intent = new Intent( AsanaListActivity.this, AsanaInfoActivity.class);
	    intent.putExtra(AsanaInfoActivity.INTENT_ASANA, asana.getId());
    	intent.putExtra(AsanaInfoActivity.INTENT_VIEW, libraryView.viewData().serialize());
	    startActivity(intent);		
	}
	
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.asanas, menu);
        return true;
    }
    
    public void resetView()
    { 
    	appPreferences.setAsanaLibraryViewPreferences(libraryView.viewData().serialize());
    	String langDescription = AsanaLibrary.getLangDescription(libraryView.viewData().getLang());
    	
    	if (libraryView.viewData().isOnlyStared()) 
    	{
    		setTitle("Favourites in "+langDescription);
    	} else 
    	{
    		if (libraryView.viewData().getFilter()!=null)
    			setTitle("Searching "+libraryView.viewData().getFilter()+" in "+langDescription);
    		else
    			setTitle("All asanas in "+langDescription);
    	}
		
		  setListAdapter(new AsanaArrayAdapter(this, libraryView));
		  ListView lv = getListView();
		  lv.setTextFilterEnabled(true);

		  final ListAdapter listAdapter = getListAdapter();

		  lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				showAsanaInfo((Asana) listAdapter .getItem(position));
			}
		  });

    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menuViewAll:
            	libraryView.viewData().setOnlyStared(false);
            	libraryView.apply();
            	resetView();
                break;
            case R.id.menuViewRandom:
            	showAsanaInfo(libraryView.asanas()[(int)(System.currentTimeMillis()%libraryView.asanas().length)]);
                break;
            case R.id.menuViewStared:
            	libraryView.viewData().setOnlyStared(true);
            	libraryView.apply();
            	resetView();
                break;
            case R.id.menuViewFilter:
            	onSearchRequested() ;
                 break;
            case R.id.menuViewIdiom:
            	libraryView.viewData().setLang(libraryView.viewData().getLang().equals(AsanaLibrary.LANG_SK)?AsanaLibrary.LANG_EN:AsanaLibrary.LANG_SK);
            	libraryView.apply();
            	resetView();
            	break;
                                     
        }
        return true;
    }
	
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        openOptionsMenu();
    }	
    
	@Override
	public boolean onSearchRequested()
	{
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Search");

		// Set an EditText view to get user input 
		final EditText input = new EditText(this);
		alert.setView(input);
		alert.setCancelable(false);
		alert.setPositiveButton("Set filter", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
		    String filter = input.getText().toString();
		    if (filter.length()>0)
		    {
			    libraryView.viewData().setFilter(filter);
			    libraryView.viewData().setOnlyStared(false);
			    libraryView.apply();
			    resetView();
		    }
		  }
		});

		alert.setNegativeButton("Remove filter", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
			    libraryView.viewData().setFilter(null);
			    libraryView.apply();
			    resetView();
		  }
		});

		alert.show();		
		return false;
	}
}
